package whf.framework.tools.generator.ui;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.SystemColor;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import whf.framework.tools.generator.cfg.Config;
import whf.framework.util.StringUtils;
import whf.framework.util.Utils;

/**
 * <p>Title: 代码生成工具</p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2006</p>
 *
 * <p>Company: whf</p>
 *
 * @author wanghaifeng
 * @version 1.0
 */
public class TablesPanel extends JPanel {
    private BorderLayout borderLayout1 = new BorderLayout();
    private JPanel jPanel2 = new JPanel();
    private JScrollPane jScrollPane1 = new JScrollPane(jPanel2);
    GridLayout gridLayout1 = new GridLayout();
    public TablesPanel() {
        try {
            jbInit();
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }

    private void jbInit() throws Exception {
        this.setLayout(borderLayout1);
        this.setBackground(SystemColor.control);
        jPanel2.setLayout(gridLayout1);
        gridLayout1.setColumns(1);
        gridLayout1.setRows(3);
        jScrollPane1.setVerticalScrollBarPolicy(JScrollPane.
                                                VERTICAL_SCROLLBAR_ALWAYS);
        this.add(jScrollPane1, java.awt.BorderLayout.CENTER);
    }
    
    public void showTables(Config config, int columnCount, String filter, boolean onlyInConfig) throws Exception {
    	if(filter == null || filter.trim().equals("*") || onlyInConfig){
    		filter = "";
    	} else {
    		filter = filter.trim().toLowerCase();
    	}
        JComponent container = jPanel2;
        java.sql.Connection con = null;
        java.sql.PreparedStatement pstmt = null;
        java.sql.ResultSet rs = null;
        Class.forName(config.getDriver());
        List<String> tables = Utils.newArrayList();
        if(onlyInConfig){
        	String[] ts = StringUtils.split(config.getTables(), ",");
        	for(String s:ts){
        		tables.add(s);
        	}
        }
        try {
            int componentCount = container.getComponentCount();
            for (int i = componentCount - 1; i >= 0; i--) {
                Component c = container.getComponent(i);
                if (c instanceof JCheckBox) {
                    container.remove(i);
                }
            }
            con = java.sql.DriverManager.getConnection(config.getUrl(), config.getUsername(), config.getPassword());
            //
            List<JCheckBox> components = new ArrayList<JCheckBox>();
            pstmt = con.prepareStatement(config.getTablesSql());
            if(config.getTablesSql().indexOf("?") >= 0)
            	pstmt.setString(1, config.getSchema());
            rs = pstmt.executeQuery();
            while (rs.next()) {
                String tableName = rs.getString(1);
                if(onlyInConfig && !tables.contains(tableName)){
                	continue;
                }
                if(!"".equals(filter) && tableName.indexOf(filter) < 0){
                	continue;
                }
                String tableComment = rs.getString(2);
                if(tableComment != null && tableComment.indexOf(";") > 0){
                	tableComment = tableComment.substring(0, tableComment.indexOf(";"));
                }
                JCheckBox item = new JCheckBox();
                item.setText(tableName + "    [" + tableComment+"]");
                item.setName(tableName);
                item.setVisible(true);
                if(onlyInConfig){
                	item.setSelected(true);
                }
                components.add(item);
            }
            int itemCount = components.size();
            for(JCheckBox component: components){
            	container.add(component);
            }
            components = null;
            this.gridLayout1.setRows(itemCount / columnCount + 1);
            container.doLayout();
            container.setSize(container.getWidth(), itemCount * 20 / columnCount);
        } finally {
            if (rs != null) rs.close();
            if (pstmt != null) pstmt.close();
            if (con != null) con.close();
        }
    }

    public String getSelectedTables() {
        JComponent container = jPanel2;
        StringBuilder sb = new StringBuilder();
        int componentCount = container.getComponentCount();
        for (int i = componentCount - 1; i >= 0; i--) {
            Component c = container.getComponent(i);
            if (c instanceof JCheckBox) {
                JCheckBox chk = (JCheckBox) c;
                if (chk.isSelected()) {
                    if (sb.length() == 0) {
                        sb.append(chk.getName());
                    } else {
                        sb.append(",").append(chk.getName());
                    }
                }
            }
        }
        return sb.toString();
    }
}
